package com.milhouz.bomberrokon.model;

import com.stickycoding.Rokon.Rokon;

/** Timer managing class */
public class Timer {

	private long currentTime = 999000;
	private int timerOriginalValue = 999;
	private boolean isRunning;
	private long startingTime;

	/** Timer constructor : time in sec */
	public Timer(int time) {
		timerOriginalValue = time;
		currentTime = time * 1000;
		isRunning = false;
	}

	/** Start the timer */
	public void start() {
		startingTime = Rokon.getTime();
		isRunning = true;
	}

	/** Get the current time in ms */
	public long getTime() {
		if (isRunning) {
			currentTime = currentTime - (Rokon.getTime() - startingTime);
			startingTime = Rokon.getTime();
			if (currentTime <= 0) {
				stop();
			}
		}
		return currentTime;
	}

	/** Get formatted String of the time */
	public String getFormatedTime() {
		return String.valueOf((int) (getTime() / 1000));
	}

	private int[] timerArray = new int[3];

	public int[] getFormatedTimeArray() {
		char[] result = getFormatedTime().toCharArray();
		timerArray = new int[3];
		for (int i = 0; i < result.length; i++) {
			timerArray[(timerArray.length - result.length) + i] = Integer.parseInt(String.valueOf(result[i]));
		}
		return timerArray;
	}

	/** Stop the timer */
	public void stop() {
		currentTime = 0;
		isRunning = false;
	}

	/** Reset the timer to the initial value */
	public void reset() {
		currentTime = timerOriginalValue * 1000;
		isRunning = false;
	}

	/** Pause the timer */
	public void pause() {
		isRunning = false;
	}

	/** Resume the timer */
	public void resume() {
		startingTime = Rokon.getTime();
		isRunning = true;
	}

	/** Indicates if the timer is running */
	public boolean isRunning() {
		return isRunning;
	}

	public int getTimerOriginalValue() {
		return timerOriginalValue;
	}

}
